package com.lyxedu.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyxedu.common.Result;
import com.lyxedu.mapper.MenuMapper;
import com.lyxedu.mapper.UserMapper;
import com.lyxedu.pojo.Menu;
import com.lyxedu.pojo.User;
import com.lyxedu.services.MenuService;
import com.lyxedu.services.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public Result roleMenus(int pageNum, int pageSize, Menu menu) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Menu::getId);
        queryWrapper.like(menu.getMenuName()!=null&&menu.getMenuName()!="",Menu::getMenuName,menu.getMenuName());
        Page<Menu> pageInfo = new Page<>(pageNum, pageSize);
        page(pageInfo,queryWrapper);
        List<Menu> menuList = pageInfo.getRecords().stream().filter(menu1 -> menu1.getPid() == null).collect(Collectors.toList());;
        for (Menu menu1 : menuList) {
            menu1.setChildren(pageInfo.getRecords().stream().filter(menu2 -> menu1.getId().equals(menu2.getPid())).collect(Collectors.toList()));

        }
        return Result.success(menuList);
    }

    @Override
    public boolean saveOrUpdateUser(Menu menu) {
        if (menu.getId() != null){
//            修改
           return updateById(menu);
        }
        return save(menu);
    }
}
